<?php
namespace iBrand\Shop\Server\Http\Controllers;

use iBrand\Component\Point\Model\Point;
use iBrand\Server\Http\Controllers\Controller;

use iBrand\Component\Order\Models\OrderItem;
use iBrand\Server\Transformers\OrdersTransformer;
use Illuminate\Http\Request;
use iBrand\Shop\Core\Repositories\OrderRepository;
use iBrand\Shop\Core\Models\Order;
use iBrand\Shop\Core\Models\ShopOrder;
use iBrand\Shop\Core\Repositories\ShopOrderRepository;

class OrderController extends Controller
{

    protected $orderRepository;
    protected $shopOrderRepository;

    public function __construct(OrderRepository $orderRepository, ShopOrderRepository $shopOrderRepository)
    {
        $this->orderRepository = $orderRepository;
        $this->shopOrderRepository = $shopOrderRepository;
    }

    public function getOrders()
    {
        $clerk = auth('clerk')->user();
        $clerk_id = $clerk->id;
        $mobile = '';
        $is_clerk_owner = 0;

        $orderConditions['channel'] = 'shop';
        $orderConditions['channel_id'] = $clerk->shop_id;

        if (request('order_no')) {
            /*$orderConditions['order_no'] = request('order_no');*/
            $itemConditions['order_no'] = ['order_no', 'like', '%' . request('order_no') . '%'];
        }

        if (request('mobile')) {
            $mobile = request('mobile');
        }

        if (request('is_clerk_owner') == 1 && $clerk->is_clerk_owner == 1) {
            $is_clerk_owner = 1;
        }

        if (request('status')) {
            $orderConditions['status'] = request('status');
        } else {
            $orderConditions ['status'] = ['status', '<>', 0];
            $orderConditions ['status2'] = ['status', '<>', 9];
        }

        $type = request('type') ? request('type') : 6;

//        $orderConditions ['user_id'] = request()->user()->id;

        $itemConditions = [];

        if (request('goods_name')) {
            $itemConditions['item_name'] = ['item_name', 'like', '%' . request('goods_name') . '%'];
        }

        if (request('goods_id')) {
            $itemConditions['item_id'] = request('goods_id');
        }

        $limit = request('limit') ? request('limit') : 15;

        if ($criteria = request('criteria')) {
            $itemConditions['order_no'] = ['order_no', 'like', '%' . $criteria . '%'];
            $itemConditions['item_name'] = ['item_name', 'like', '%' . $criteria . '%'];
            $itemConditions['item_id'] = ['item_id', 'like', '%' . $criteria . '%'];

            $order = $this->orderRepository->getOrdersByCriteria($orderConditions, $itemConditions, $limit, $type, $clerk_id, $mobile, $is_clerk_owner);

        } else {

            $order = $this->orderRepository->getOrdersByConditions($orderConditions, $itemConditions, $limit, ['items', 'shippings'], $type, $clerk_id, $mobile, $is_clerk_owner);
        }


        return $this->response()->paginator($order, new \iBrand\Shop\Server\Transformers\OrdersTransformer());

    }


    public function getOrderDetails($order_no)
    {

        $clerk = auth('clerk')->user();
        if (!$order_no || !$order = $this->orderRepository->getOrderByNo($order_no)) {
            return $this->response()->errorBadRequest('订单不存在');
        }


        if (!$this->shopOrderRepository->getOrderInfoByClerk($clerk, $order->id)) {
            return $this->api([], false, 400, '无权操作此订单');
        };


        $details = $this->orderRepository->getShopOrderByNo($order_no);
        if ($refund = $details->orderRefund) {
            $details->total = $refund->difference_amount;
            $details->total_yuan = number_format($refund->difference_amount / 100, 2, ".", "");
        }

        $item_ids = $order->items->pluck('id')->toArray();
        $point = Point::where(['action' => 'order_item'])->whereIn('item_id', $item_ids)->get()->sum('value');           //本次获得积分
        $total_point = $order->user->available_integral;    //累计积分
        $guide = $order->shopOrder->clerk->name;
        $shop = $order->shopOrder->shop;
        $img = [
            'logo' => settings('shop_show_logo'),
            'wechat' => settings('shop_wechat_qrcode')
        ];

        return $this->response()->item($details, new OrdersTransformer())->setMeta([
            'point' => $point,
            'total_point' => $total_point,
            'guide' => $guide,
            'shop' => $shop,
            'img' => $img
        ]);
    }



    public function uploadTicket()
    {
        $order_no = request('order_no');
        $images = request('images');
        if (!$order_no || !$images) {
            return $this->api([], false, 405, '');
        }
        if (!$order = $this->orderRepository->getOrderByNo($order_no)) {
            return $this->response()->errorBadRequest('订单不存在');
        }

        $clerk = auth('clerk')->user();

        if (!$shopOrder = $this->shopOrderRepository->getOrderInfoByClerk($clerk, $order->id)) {
            return $this->api([], false, 400, '无权操作此订单');
        };

        if ($this->shopOrderRepository->update(['images' => json_encode($images)], $shopOrder->id)) {
            return $this->api([], true, 200, '上传小票成功');
        };

        return $this->api([], false, 405, '上传小票失败');

    }


}